//
//  ContactUsViewController.swift
//  SitPosition
//
//  Created by apple on 2019/4/14.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


let kCellIdentifier: String = "ContactUsCell"
let kPhoneNumber: String = "13968916057"
let kEmail: String = "67861851@163.com"

class ContactUsViewController : GKNavigationBarViewController {
    
    // 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryDarkTextColor
        label.font = Theme.Font.boldFont(ofSize: 26)
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        label.text = Strings.contactUs
        return label
    }()
    
    // 列表
    lazy var tableView: UITableView = {
        let table = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        table.separatorColor = Theme.Color.primaryDarkSeperatorColor
        table.separatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
        table.rowHeight = 64
        table.dataSource = self
        table.delegate = self
        
        let footerLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 290, height: 64))
        footerLabel.backgroundColor = UIColor.clear
        footerLabel.font = Theme.Font.regularFont(ofSize: 12)
        footerLabel.textColor = Theme.Color.lighgrayTextColor
        footerLabel.lineBreakMode = .byWordWrapping
        footerLabel.textAlignment = .center
        footerLabel.numberOfLines = 0
        footerLabel.text = Strings.contactAnyQuestion
        table.tableFooterView = footerLabel
        
        return table
    }()
    
    // 数据源
    lazy var dataSource: [(String, String, (() -> Void))] = [
        (Strings.contactPhone, kPhoneNumber, { self._callPhone(kPhoneNumber) } ),
        (Strings.contactWeChat, kPhoneNumber,  { } ),
        (Strings.contactMail, kEmail, { self._sendEmail(kEmail) })
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 导航栏
        gk_statusBarStyle = .default
        gk_navBackgroundColor = UIColor.clear
        gk_navLeftBarButtonItem = UIBarButtonItem(imageName: "ic_nav_back_black", target: self, action: #selector(_backButtonTapped))
        
        // 子视图
        view.addSubviews([
            titleLabel,
            tableView,
            ])
        _setupContraints()
        view.backgroundColor = UIColor.white
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.default
    }
}

// MARK: - UITableViewDataSource/UITableViewDelegate

extension ContactUsViewController : UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell
        if let reusedCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: kCellIdentifier) {
            cell = reusedCell
        } else {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: kCellIdentifier)
            cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
            
            cell.textLabel?.font = Theme.Font.mediumFont(ofSize: 16)
            cell.textLabel?.textColor = Theme.Color.primaryDarkTextColor
            cell.textLabel?.lineBreakMode = .byTruncatingTail
            cell.textLabel?.textAlignment = .left
            
            cell.detailTextLabel?.font = Theme.Font.regularFont(ofSize: 12)
            cell.detailTextLabel?.textColor = Theme.Color.lighgrayTextColor
            cell.detailTextLabel?.lineBreakMode = .byWordWrapping
            cell.detailTextLabel?.textAlignment = .left
        }
        let (title, subtitle, _) = dataSource[indexPath.row]
        cell.textLabel?.text = title
        cell.detailTextLabel?.text = subtitle
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let (_, _, callback) = dataSource[indexPath.row]
        callback()
    }
}

extension ContactUsViewController {
    
    // 设置视图约束
    func _setupContraints() {
        // titleLabel
        constrain(titleLabel, gk_navigationBar, view) { v, sibling, parent in
            v.top == sibling.bottom + 19
            v.left == parent.left + 16
            v.right == parent.right - 16
            v.height == 25
        }
        
        // tableView
        constrain(tableView, titleLabel, view) { v, sibling, parent in
            v.top == sibling.bottom
            v.left == parent.left
            v.right == parent.right
            v.bottom == parent.bottom
        }
    }
    
    // 返回按钮点击事件
    @objc func _backButtonTapped() {
        if presentingViewController != nil {
            dismiss(animated: true, completion: nil)
        } else {
            navigationController?.popViewController(animated: true)
        }
    }
    
    // 拨打电话
    func _callPhone(_ phone: String) {
        let urlString: String = "tel://\(phone)"
        guard let url = URL(string: urlString) else { return }
        UIApplication.shared.open(url, options: [ : ], completionHandler: nil)
    }
    
    // 发送email
    func _sendEmail(_ email: String) {
        let urlString: String = "mailto://\(email)"
        guard let url = URL(string: urlString) else { return }
        UIApplication.shared.open(url, options: [ : ], completionHandler: nil)
    }
}
